package com.yangxiangrice.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yangxiangrice.pojo.*;
import com.yangxiangrice.service.ProdPlanService;
import com.yangxiangrice.service.SeedListService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @Description: TODO
 * @author: shixiaojian
 * @date: 2021年11月22日 12:49
 */

@Controller
@RequestMapping("/prodplan")
@CrossOrigin
@Slf4j
public class ProdPlanController {

    @Autowired
    ProdPlanService prodPlanService;
    @Autowired
    SeedListService seedListService;

    @GetMapping(value = "/getseed")
    @ResponseBody
    public Result getseed() {
        Result<List> result = new Result<>();
        Option option = null;
        List<Option> res = new ArrayList<>();
        List<SeedList> list = seedListService.list();
        for (SeedList seedList : list) {
            option = new Option();
            option.setLabel(seedList.getSeedName());
            option.setValue(seedList.getSeedName());
            res.add(option);
        }
        return result.ok(res);
    }


    @GetMapping(value = "/listpage")
    @ResponseBody
    public Result listpage(@RequestParam("page") int page,
                           @RequestParam("limit") int limit) {
        Result<IPage> result = new Result<>();
        Subject subject = SecurityUtils.getSubject();
        String phone = subject.getPrincipal().toString();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("phone", phone);
        IPage<ProdPlan> prodPlanIPage = prodPlanService.seletePage(new Page<>(page, limit), wrapper);
        return result.ok(prodPlanIPage);
    }

    @PostMapping(value = "/save")
    @ResponseBody
    public Result save(@RequestBody ProdPlan prodPlan) {
        Result<Boolean> result = new Result<>();
        prodPlan.setCreatetime(new Timestamp(System.currentTimeMillis()));
        if (ObjectUtil.isEmpty(prodPlan.getPlanId())) {
            Subject subject = SecurityUtils.getSubject();
            String phone = subject.getPrincipal().toString();
            String year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
            String planCode = "";
            planCode += year + "年度购种计划" + prodPlan.getSeedName();
            prodPlan.setPlanCode(planCode);
            prodPlan.setPhone(phone);
            boolean tag = prodPlanService.save(prodPlan);
            if (tag) {
                result.ok(tag);
            } else {
                result.fail("添加失败");
            }
        }else{
            boolean tag = prodPlanService.updateById(prodPlan);
            if (tag) {
                result.ok(tag);
            } else {
                result.fail("更新失败");
            }
        }
/*        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("seedName", prodPlan.getSeedName());*/
        return result;
    }

    @PostMapping(value = "/delete")
    @ResponseBody
    public Result delete(@RequestParam(value = "planId") Integer planId) {
        Result<Boolean> result = new Result<>();
        boolean tag = prodPlanService.removeById(planId);
        if (tag) {
            result.ok(tag);
        } else {
            result.fail("删除失败");
        }
        return result;
    }

    @PostMapping(value = "/deleteBatch")
    @ResponseBody
    public Result deleteBatch(@RequestBody int[] ids) {
        ArrayList<Integer> list = new ArrayList<>();
        Result<Boolean> result = new Result<>();
        for (int id : ids) {
            list.add(id);
        }
        boolean tag = prodPlanService.removeByIds(list);
        if (tag) {
            result.ok(tag);
        } else {
            result.fail("删除失败");
        }
        return result;
    }

    @RequestMapping(value = "/search")
    @ResponseBody
    public Result search(@RequestParam("input") String input,
                         @RequestParam("page") int page,
                         @RequestParam("limit") int limit) {
        Result<IPage> result = new Result<>();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("planCode", input);
        IPage<ProdPlan> prodPlanIPage = prodPlanService.seletePage(new Page<>(page, limit), wrapper);
        if (ObjectUtil.isEmpty(prodPlanIPage.getRecords())) {
            result.fail("未找到符合条件的数据");
        } else {
            result.ok(prodPlanIPage);
        }
        return result;
    }
}
